package com.simtop.controller;

import com.alibaba.fastjson.JSONObject;
import com.simtop.annotation.RepeatSubmit;
import com.simtop.aop.SystemControllerLog;
import com.simtop.domain.news.PolicyInformation;
import com.simtop.domain.news.PolicyInformationVo;
import com.simtop.service.PolicyInformationService;
import com.simtop.utils.ResultBean;
import com.simtop.utils.convert.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * @author lvyongwei
 */
@RestController
@RequestMapping("/policyInformation")
public class PolicyInformationController extends BaseController{

    @Autowired
    private PolicyInformationService policyInformationService;

    /**
     * 获取新闻资讯
     * @param policyInformation
     * @return
     */
    @PostMapping("/getPolicyInformation")
    public ResultBean getPolicyInformation(@RequestBody PolicyInformation policyInformation){
        if(policyInformation.getIfPage() != null && policyInformation.getIfPage() == false){
            return ResultBean.success("查询成功",policyInformationService.getPolicyInformationList(policyInformation));
        }
        startPage(policyInformation);
        List<PolicyInformationVo> policyInformationList = policyInformationService.getPolicyInformationList(policyInformation);
        return ResultBean.success("获取内容成功",getDataTable(policyInformationList));
    }

    /**
     * 添加新闻资讯内容
     * @param policyInformation
     * @return
     */
    @SystemControllerLog(description = "添加新闻资讯内容")
    @PostMapping("/insertPolicyInformation")
    @RepeatSubmit
    public ResultBean insertPolicyInformation(@Validated PolicyInformation policyInformation,
                                              @NotNull(message = "必须上传封面") MultipartFile photo,
                                              List<MultipartFile> files){
        return ResultBean.toResult(policyInformationService.insertPolicyInformation(policyInformation,photo,files));
    }

    /**
     * 修改新闻资讯
     * @param policyInformation
     * @param photo
     * @param files
     * @return
     */
    @SystemControllerLog(description = "修改新闻资讯内容")
    @PostMapping("/updatePolicyInformation")
    @RepeatSubmit
    public ResultBean updatePolicyInformation(PolicyInformation policyInformation,
                                              MultipartFile photo,
                                              List<MultipartFile> files) {
        return ResultBean.toResult(policyInformationService.updatePolicyInformation(policyInformation,photo,files));
    }

    /**
     * 删除新闻资讯
     * @return
     */
    @SystemControllerLog(description = "删除新闻资讯内容")
    @PostMapping("/deletePolicyInformation")
    public ResultBean deletePolicyInformation(@RequestBody @NotBlank(message = "参数不允许为空") String data){
        /**
         * 转数组
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        String ids = jsonObject.getString("ids");
        Long[] idArray = Convert.toLongArray(ids);
        List<Long> list = Arrays.asList(idArray);
        return ResultBean.toResult(policyInformationService.deletePolicyInformation(list));
    }

    /**
     * 根据id获取资讯
     * @param data
     * @return
     */
    @PostMapping("/getPolicyById")
    public ResultBean getPolicyInformationById(@RequestBody @NotBlank(message = "参数不允许为空") String data){
        JSONObject jsonObject = JSONObject.parseObject(data);
        Long id = jsonObject.getLong("policyInformationId");
        return ResultBean.success("获取内容成功",policyInformationService.getPolicyInformationById(id));
    }
}
